<?php
/**
 * 参考：http://qtip2.com/options
 * @author ycoe
 *
 */
class YSelectorWidget extends YWidget{
	public $id = null;
	public $name = null;
	public $class = null;
	public $default_text = null;	//未选择时，界面显示的内容
	public $value = null;
	public $value_text = null;
	public $options = null;
	public $title = '请选择';
	public $tips_text = null;
	public $delay = 1000;
	public $modal = true;
	public $on_show = null;
	public $body = null;
	public $rule = null;
	
	private $html_options = array();
	public function run(){
		//自动加载资源
		$this->load_resource();
		
		$this->html_options['id'] = $this->id;
		if(!empty($this->class)){
			$this->html_options['class'] = 'y_selector ' . $this->class;
		}else{
			$this->html_options['class'] = 'y_selector';
		}
		if($this->rule !== null){
			$this->html_options['rule'] = $this->rule;
		}
		
		$this->cs->registerCssFile(BASE_URL . '/js/jquery_plus/qTip/jquery.qtip.min.css');
		$this->cs->registerScriptFile(BASE_URL . '/js/jquery_plus/qTip/jquery.qtip.min.js', CClientScript::POS_END);
	
		echo CHtml::tag('div', $this->html_options, false, false);
		echo CHtml::tag('span', array('title'=>$this->default_text), empty($this->value_text)?$this->default_text:$this->value_text);
		if(is_array($this->value)){
			foreach ($this->value as $v){
				echo '<input type="hidden" name="' . $this->name . '" value="' . $v . '"/>';
			}
		}else{
			echo '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
		}
		echo CHtml::closeTag('div');
		
		$tpl_file = $this->class_name . '/' . $this->tpl . '.php';
		if(file_exists(dirname(__FILE__) . '/views/' . $tpl_file)){
// 			$this->$type_action();
			$this->body = $this->render($this->class_name . '/' . $this->tpl, null, true);
		}else{
// 			$html = '参数错误，请检查Widget的type属性！';
			$this->body = '模板文件不存在：' . dirname(__FILE__) . 'views/' . $tpl_file;
		}
		
		$opts = array(
			'show' 	=> array(
				'event'=>'click',
				'solo' => true,
				'modal' => $this->modal,
			),
			'hide'	=> 'unfocus',
			'position'	=> array(
				'my' => 'left top',
				'at' => 'top right',
				'adjust' => array('resize'=>true)
			),
			'content'	=> array(
				'title' => $this->title,
				'text'	=> $this->body,
				'button'=> 'Close',
			),
			'style'	=> array(
				'classes' 	=> 'qtip-bootstrap qtip_' . $this->tpl,
			),
// 			'events' => array(
// 				'show' 		=> '',
// 				'render' 	=> '',
// 				'move' 		=> '',
// 				'hide' 		=> '',
// 				'toggle' 	=> '',
// 				'visible' 	=> '',
// 				'focus' 	=> '',
// 				'blur' 		=> '',
// 			)
		);
		
		if($this->on_show !== null){
			$opts['events']['show'] = '__ON_SHOW__';
		}
		$conf = json_encode($opts);
		if($this->on_show !== null){
			$conf = preg_replace('/"__ON_SHOW__"/', $this->on_show, $conf);
		}
		
		$this->cs->registerScript($this->id, 
			'$("#' . $this->id . '").qtip(' . $conf . ');'
		);
	}
}

?>